from django.contrib import admin
from celery_tasks.static_html.tasks import generate_static_list_search_html,generate_static_sku_detail_html
# Register your models here.
from goods import models

class GoodsModelAdmin(admin.ModelAdmin):
    list_display = ['id','name']

# 商品类别操作
class GoodsCategoryAdmin(admin.ModelAdmin):
    # save_model是ModelAdmin的方法，原方法中只做了obj.save()操作
    # obj代表的是models中的GoodsChannel模型类对象
    def save_model(self, request, obj, form, change):
        print(obj)
        print(type(obj))

        obj.save()
        # 保存后可以做生成静态化文件操作，可以更新页面数据
        generate_static_list_search_html.delay()

    def delete_model(self, request, obj):
        obj.delete()
        generate_static_list_search_html.delay()


class SkuAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        print(obj,obj.id)
        obj.save()
        generate_static_sku_detail_html.delay(obj.id)

    # def delete_model(self, request, obj):

class SKUSpecificationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        generate_static_sku_detail_html.delay(obj.sku.id)

    def delete_model(self, request, obj):
        obj.delete()
        generate_static_sku_detail_html.delay(obj.sku.id)


class SKUImageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        generate_static_sku_detail_html.delay(obj.sku.id)

        if not obj.sku.default_image_url:
            obj.sku.default_image_url = obj.image
            obj.sku.save()

    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        generate_static_sku_detail_html.delay(sku_id)


admin.site.register(models.GoodsCategory,GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Goods,GoodsModelAdmin)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU,SkuAdmin)
admin.site.register(models.SKUSpecification,SKUSpecificationAdmin)
admin.site.register(models.SKUImage,SKUImageAdmin)

